﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using ZK.HardWareSDK;

namespace ZK.HardWareBLL
{
    //Onbon6E1XCardNewBLL
    public class Onbon6E1XCardNewBLL : BaseBLL
    {
        OnbonBx_sdk_dualNewSDK.EQareaHeader_G6 aheader;
        OnbonBx_sdk_dualNewSDK.EQSound_6G stSoundData = new OnbonBx_sdk_dualNewSDK.EQSound_6G();
        OnbonBx_sdk_dualNewSDK.EQpageHeader_G6 pheader;
        OnbonBx_sdk_dualNewSDK.DynamicAreaParams[] Params = new OnbonBx_sdk_dualNewSDK.DynamicAreaParams[1];
        public bool IsInit = false;

        public Onbon6E1XCardNewBLL(Models.TerminalM terminal)
        {
            this.terminal = terminal;
            if (!IsInit)
            {
                InitSdk(terminal);
            }

        }
        public int InitSdk(Models.TerminalM terminal)
        {
            IsInit = true;
            {
                aheader.AreaType = 0x00;
                aheader.AreaX = 0;
                aheader.AreaY = 0;
                aheader.AreaWidth = (ushort)terminal.width;
                aheader.AreaHeight = (ushort)terminal.height;
                aheader.BackGroundFlag = 0x00;
                aheader.Transparency = 101;
                aheader.AreaEqual = 0x00;
            }

            {
                stSoundData.SoundFlag = 0;
                stSoundData.SoundVolum = 0;
                stSoundData.SoundSpeed = 0;
                stSoundData.SoundDataMode = 0;
                stSoundData.SoundReplayTimes = 0;
                stSoundData.SoundReplayDelay = 0;
                stSoundData.SoundReservedParaLen = 0;
                stSoundData.Soundnumdeal = 0;
                stSoundData.Soundlanguages = 0;
                stSoundData.Soundwordstyle = 0;
                stSoundData.SoundDataLen = 0;
                byte[] t = new byte[1];
                t[0] = 0;
                stSoundData.SoundData = t;
            }

            {
                pheader.PageStyle = 0x00;
                pheader.DisplayMode = 0x02;
                pheader.ClearMode = 0x01;
                pheader.Speed = 15;
                pheader.StayTime = 500;
                pheader.RepeatTime = 1;
                pheader.ValidLen = 0;
                pheader.CartoonFrameRate = 0x00;
                pheader.BackNotValidFlag = 0x00;
                pheader.arrMode = OnbonBx_sdk_dualNewSDK.E_arrMode.eMULTILINE;
                pheader.fontSize = 9;
                pheader.color = (uint)0x01;
                pheader.fontBold = 1;
                pheader.fontItalic = 0;
                pheader.tdirection = OnbonBx_sdk_dualNewSDK.E_txtDirection.pNORMAL;
                pheader.txtSpace = 0;
                pheader.Valign = 2;
                pheader.Halign = 2;
            }



            return OnbonBx_sdk_dualNewSDK.bxDual_InitSdk();
        }

        /// <summary>
        /// 更新动态区
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="ipStr"></param>
        public bool sendMessage(string msg)
        {
            try
            {
                string ipStr = terminal.ip;
                int err = 0;
                byte[] ip = Encoding.Default.GetBytes(ipStr);
                byte[] str = Encoding.Default.GetBytes(msg);
                byte[] font = Encoding.Default.GetBytes("黑体");
                aheader.stSoundData = stSoundData;
                IntPtr aa = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(OnbonBx_sdk_dualNewSDK.EQareaHeader_G6)));
                Marshal.StructureToPtr(aheader, aa, false);
                IntPtr bb = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(OnbonBx_sdk_dualNewSDK.EQpageHeader_G6)));
                Marshal.StructureToPtr(pheader, bb, false);

                //更新动态区，可设置属性 zc
                err = OnbonBx_sdk_dualNewSDK.bxDual_dynamicArea_AddAreaTxtDetails_6G(ip, 5005, OnbonBx_sdk_dualNewSDK.E_ScreenColor_G56.eSCREEN_COLOR_SINGLE, 0,
                    aa, bb, font, str);
                Params[0].uAreaId = 0;
                Params[0].oAreaHeader_G6 = aheader;
                Params[0].stPageHeader = pheader;
                Params[0].fontName = BytesToIntptr(font);
                Params[0].strAreaTxtContent = BytesToIntptr(str);
            }
            catch (Exception ex)
            {

                Common.Log4.LogManage.WriteErr(ex.Message);
                return false;
            }
            return true;
        }
        /// <summary>
        /// 更新动态区
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="ipStr"></param>
        public bool sendMessage(string msg, string cip)
        {
            try
            {
                string ipStr = cip;
                int err = 0;
                byte[] ip = Encoding.Default.GetBytes(ipStr);
                byte[] str = Encoding.Default.GetBytes(msg);
                byte[] font = Encoding.Default.GetBytes("黑体");
                aheader.stSoundData = stSoundData;
                IntPtr aa = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(OnbonBx_sdk_dualNewSDK.EQareaHeader_G6)));
                Marshal.StructureToPtr(aheader, aa, false);
                IntPtr bb = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(OnbonBx_sdk_dualNewSDK.EQpageHeader_G6)));
                Marshal.StructureToPtr(pheader, bb, false);

                //更新动态区，可设置属性 zc
                err = OnbonBx_sdk_dualNewSDK.bxDual_dynamicArea_AddAreaTxtDetails_6G(ip, 5005, OnbonBx_sdk_dualNewSDK.E_ScreenColor_G56.eSCREEN_COLOR_SINGLE, 0,
                    aa, bb, font, str);
                Params[0].uAreaId = 0;
                Params[0].oAreaHeader_G6 = aheader;
                Params[0].stPageHeader = pheader;
                Params[0].fontName = BytesToIntptr(font);
                Params[0].strAreaTxtContent = BytesToIntptr(str);
            }
            catch (Exception ex)
            {

                Common.Log4.LogManage.WriteErr(ex.Message);
                return false;
            }
            return true;
        }


        //byte[]转换为Intptr
        public static IntPtr BytesToIntptr(byte[] bytes)
        {
            int size = bytes.Length;
            IntPtr buffer = Marshal.AllocHGlobal(size);
            try
            {
                Marshal.Copy(bytes, 0, buffer, size);
                return buffer;
            }
            finally
            {
                //Marshal.FreeHGlobal(buffer);
            }
        }


        public override bool LedSengMessage(string str)
        {
            return this.sendMessage(str);
        }
    }

}
